package com.ttxs.fileformathandling.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttxs.apicommon.model.entity.User;
import com.ttxs.fileformathandling.annotation.AuthCheck;
import com.ttxs.fileformathandling.common.BaseResponse;
import com.ttxs.fileformathandling.common.DeleteRequest;
import com.ttxs.fileformathandling.common.ErrorCode;
import com.ttxs.fileformathandling.common.ResultUtils;
import com.ttxs.fileformathandling.constant.UserConstant;
import com.ttxs.fileformathandling.exception.BusinessException;
import com.ttxs.fileformathandling.exception.ThrowUtils;

import com.ttxs.fileformathandling.model.dto.file.FileRecordQueryRequest;
import com.ttxs.fileformathandling.model.dto.file.FileRecordUpdateRequest;

import com.ttxs.fileformathandling.model.entity.FileRecord;
import com.ttxs.fileformathandling.model.vo.FileRecordVO;
import com.ttxs.fileformathandling.service.FileRecordService;
import com.ttxs.fileformathandling.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文件列表接口
 *
 * @author cwt
 * @version 1.0
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileRecordController {
    @Resource
    private FileRecordService fileRecordService;


    @Resource
    private UserService userService;


    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteRecord(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        FileRecord oldFileRecord = fileRecordService.getById(id);
        ThrowUtils.throwIf(oldFileRecord == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldFileRecord.getUserId().equals(user.getId()) && userService.notAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = fileRecordService.removeById(id);
        return ResultUtils.success(b);
    }


    /**
     * 更新（仅管理员）
     *
     * @param fileRecordUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateRecord(@RequestBody FileRecordUpdateRequest fileRecordUpdateRequest) {
        if (fileRecordUpdateRequest == null || fileRecordUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        FileRecord fileRecord = new FileRecord();
        BeanUtils.copyProperties(fileRecordUpdateRequest, fileRecord);

        // 参数校验
        fileRecordService.validApi(fileRecord, false);
        long id = fileRecordUpdateRequest.getId();
        // 判断是否存在
        FileRecord oldFileRecord = fileRecordService.getById(id);
        ThrowUtils.throwIf(oldFileRecord == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = fileRecordService.updateById(oldFileRecord);
        return ResultUtils.success(result);
    }

    /**
     * 用户信息分页
     *
     * @param fileRecordQueryRequest 用户插叙请求
     * @param request                请求
     * @return 用户信息页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<FileRecord>> listRecordByPage(@RequestBody FileRecordQueryRequest fileRecordQueryRequest, HttpServletRequest request) {
        long current = fileRecordQueryRequest.getCurrent();
        long pageSize = fileRecordQueryRequest.getPageSize();
        Page<FileRecord> recordPage = fileRecordService.page(new Page<>(current, pageSize), fileRecordService.getQueryWrapper(fileRecordQueryRequest, request));
        return ResultUtils.success(recordPage);
    }

    /**
     * 列出用户部分信息
     *
     * @param fileRecordQueryRequest 用户查询请求
     * @param request                请求体
     * @return 用户部分信息
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<FileRecordVO>> listRecordVOByPage(@RequestBody FileRecordQueryRequest fileRecordQueryRequest, HttpServletRequest request) {
        if (fileRecordQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = fileRecordQueryRequest.getCurrent();
        long pageSize = fileRecordQueryRequest.getPageSize();
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        Page<FileRecord> recordPage = fileRecordService.page(new Page<>(current, pageSize), fileRecordService.getQueryWrapper(fileRecordQueryRequest, request));
        Page<FileRecordVO> recordVOPage = new Page<>(current, pageSize, recordPage.getTotal());
        List<FileRecordVO> recordVOList = fileRecordService.getRecordVOList(recordPage.getRecords());
        recordVOPage.setRecords(recordVOList);
        return ResultUtils.success(recordVOPage);
    }
}
